package Kalkulator;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import tools.koneksi;

public class KalkulatorKontrol {

    private Connection conn;
    String url = "jdbc:oracle:thin:@localhost:1521:xe";
    String user = "HR";
    String pwd = "renia";

//      String url  = "jdbc:oracle:thin:@localhost:1521:xe";
//      String user = "MHs115314078";
//      String pwd  = "mhs115314078";
//    String url = "jdbc:oracle:thin:@localhost:1521:xe";
//    String user = "hr";
//    String pwd = "hr";
    public KalkulatorKontrol(Connection koneksi) {
        this.conn = koneksi;
    }

    public static KalkulatorKontrol getKoneksiKalkulator() throws SQLException {
        KalkulatorKontrol kon = new KalkulatorKontrol(koneksi.bukaKoneksi());
        return kon;
    }

    public KalkulatorKontrol() {
    }

    public double hitungIMT(double bb, double tb) {
        Kalkulator kalku = new Kalkulator();
        kalku.setBb(bb);
        kalku.setTb(tb);
        //convert tinggi dan berat badan ke meter
        double tbMeter = (kalku.getTb() / 100);
        kalku.setTb(tbMeter);
        double imt = kalku.getBb() / (kalku.getTb() * kalku.getTb());
        kalku.setImt(imt);
        return kalku.getImt();
    }

    public String hasilIMT(double imt) {
        Kalkulator kalku = new Kalkulator();
        kalku.setImt(imt);
        String hasilIMT;
        if (kalku.getImt() < 17) {
            hasilIMT = "Sangat Kurus";
        } else {
            if ((kalku.getImt() >= 17) && (kalku.getImt() < 18.5)) {
                hasilIMT = "Kurus";
            } else {
                if ((kalku.getImt() >= 18.5) && (kalku.getImt() <= 25.5)) {
                    hasilIMT = "Normal";
                } else {
                    if ((kalku.getImt() > 25) && (kalku.getImt() <= 27)) {
                        hasilIMT = "Gemuk";
                    } else {
                        hasilIMT = "Sangat Gemuk";
                    }
                }
            }
        }
        kalku.setHasilIMT(hasilIMT);
        return kalku.getHasilIMT();
    }

    public double hitungBBR(double bb, double tb) {
        Kalkulator kalku = new Kalkulator();
        kalku.setBb(bb);
        kalku.setTb(tb);
        double hitungBBR = (kalku.getBb() / (kalku.getTb() - 100)) * 100;
        kalku.setBbr(hitungBBR);
        return hitungBBR;
    }

    public String hasilBBR(double bbr) {
        Kalkulator kalku = new Kalkulator();
        kalku.setBbr(bbr);
        String hasilBBR;
        if (bbr < 90) {
            hasilBBR = "Underweight / Gizi Kurang";
        } else {
            if ((bbr >= 90) && (bbr <= 110)) {
                hasilBBR = "Normal / Gizi Baik";
            } else {
                if ((bbr > 110) && (bbr <= 120)) {
                    hasilBBR = "Overweight";
                } else {
                    hasilBBR = "Obesitas / Kegemukan";
                }
            }
        }
        kalku.setHasilBBR(hasilBBR);
        return kalku.getHasilBBR();
    }

    // hitung BBI utk usia < 10thn
    public double hitungBBI_1(int usia) {
        Kalkulator kalku = new Kalkulator();
        kalku.setUsia(usia);
        double bbi = kalku.getUsia() * 2 + 8;
        kalku.setBbi(bbi);
        return kalku.getBbi();
    }

    // hitung BBI utk usia > 10thn & cowok & tinggi < 160cm
    public double hitungBBI_2L(double tb) {
        Kalkulator kalku = new Kalkulator();
        kalku.setTb(tb);
        double bbi = kalku.getTb() - 100;
        kalku.setBbi(bbi);
        return kalku.getBbi();
    }

    // hitung BBI utk usia > 10thn & cewek & tinggi < 150cm
    public double hitungBBI_2P(double tb) {
        Kalkulator kalku = new Kalkulator();
        kalku.setTb(tb);
        double bbi = kalku.getTb() - 100;
        kalku.setBbi(bbi);
        return kalku.getBbi();
    }

    // hitung BBI utk usia > 10thn & cowok tinggi > 160cm & cewek > 150cm
    public double hitungBBI_3(double tb) {
        Kalkulator kalku = new Kalkulator();
        kalku.setTb(tb);
        double bbi = (0.9) * (kalku.getTb() - 100);
        kalku.setBbi(bbi);
        return kalku.getBbi();
    }

    // hitung BMR laki-laki usia 0 - 3 tahun
    public double hitungBMRCo0_3(double bbi) {
        Kalkulator kalku = new Kalkulator();
        kalku.setBbi(bbi);
        double bmr = (60.9 * kalku.getBbi()) - 54;
        kalku.setBmr(bmr);
        return kalku.getBmr();
    }

    // hitung BMR perempuan usia 0 - 3 tahun
    public double hitungBMRCe0_3(double bbi) {
        Kalkulator kalku = new Kalkulator();
        kalku.setBbi(bbi);
        double bmr = (61 * kalku.getBbi()) - 51;
        kalku.setBmr(bmr);
        return kalku.getBmr();
    }

    // hitung BMR laki-laki usia >3 - <10 tahun
    public double hitungBMRCo3_10(double bbi) {
        Kalkulator kalku = new Kalkulator();
        kalku.setBbi(bbi);
        double bmr = (22.7 * kalku.getBbi()) + 495;
        kalku.setBmr(bmr);
        return kalku.getBmr();
    }

    // hitung BMR perempuan usia >3 - <10 tahun
    public double hitungBMRCe3_10(double bbi) {
        Kalkulator kalku = new Kalkulator();
        kalku.setBbi(bbi);
        double bmr = (22.5 * kalku.getBbi()) + 499;
        kalku.setBmr(bmr);
        return kalku.getBmr();
    }

    // hitung BMR laki-laki usia >10 - 18 tahun
    public double hitungBMRCo10_18(double bbi) {
        Kalkulator kalku = new Kalkulator();
        kalku.setBbi(bbi);
        double bmr = (17.5 * kalku.getBbi()) + 651;
        kalku.setBmr(bmr);
        return kalku.getBmr();
    }

    // hitung BMR perempuan usia >10 - 18 tahun
    public double hitungBMRCe10_18(double bbi) {
        Kalkulator kalku = new Kalkulator();
        kalku.setBbi(bbi);
        double bmr = (12.2 * kalku.getBbi()) + 746;
        kalku.setBmr(bmr);
        return kalku.getBmr();
    }

    // hitung BMR laki-laki usia >18 tahun
    public double hitungBMRCo_18(int usia, double bbi, double tb) {
        Kalkulator kalku = new Kalkulator();
        kalku.setUsia(usia);
        kalku.setBbi(bbi);
        kalku.setTb(tb);
        double bmr = 66.5 + (13.75 * kalku.getBbi()) + (5 * kalku.getTb()) - (6.8 * kalku.getUsia());
        kalku.setBmr(bmr);
        return kalku.getBmr();
    }

    // hitung BMR perempuan usia >18 tahun
    public double hitungBMRCe_18(int usia, double bbi, double tb) {
        Kalkulator kalku = new Kalkulator();
        kalku.setUsia(usia);
        kalku.setBbi(bbi);
        kalku.setTb(tb);
        double bmr = 655.1 + (9.56 * kalku.getBbi()) + (1.85 * kalku.getTb()) - (4.68 * kalku.getUsia());
        kalku.setBmr(bmr);
        return kalku.getBmr();
    }

    // hitung energi biasa
    public double hitungEnergi(double bmr, int pa) {
        Kalkulator kalku = new Kalkulator();
        kalku.setBmr(bmr);
        kalku.setPa(pa);
        double energi = 0;
        if (kalku.getPa() == 1) {
            kalku.setIndexPa(2.0);
            energi = kalku.getBmr() * kalku.getIndexPa();
            kalku.setEnergi(energi);
        } else if (kalku.getPa() == 2) {
            kalku.setIndexPa(1.75);
            energi = kalku.getBmr() * kalku.getIndexPa();
            kalku.setEnergi(energi);
        } else if (kalku.getPa() == 3) {
            kalku.setIndexPa(1.5);
            energi = kalku.getBmr() * kalku.getIndexPa();
            kalku.setEnergi(energi);
        } else if (kalku.getPa() == 4) {
            kalku.setIndexPa(1.3);
            energi = kalku.getBmr() * kalku.getIndexPa();
            kalku.setEnergi(energi);
        }
        return kalku.getEnergi();
    }

    // hitung energi ibu hamil
    public double hitungEnergiBuMil(double energi, String trisem) {
        Kalkulator kalku = new Kalkulator();
        kalku.setEnergi(energi);
        kalku.setTrisem(trisem);
        double energiBuMil = 0;
        if (kalku.getTrisem().equals("ts1")) {
            energiBuMil = kalku.getEnergi() + 180;
            kalku.setEnergi(energiBuMil);
        } else if (kalku.getTrisem().equals("ts2")) {
            energiBuMil = kalku.getEnergi() + 300;
            kalku.setEnergi(energiBuMil);
        } else if (kalku.getTrisem().equals("ts3")) {
            energiBuMil = kalku.getEnergi() + 300;
            kalku.setEnergi(energiBuMil);
        }
        return kalku.getEnergi();
    }

    // hitung energi ibu menyusui
    public double hitungEnergiBuSui(double energi, String sem) {
        Kalkulator kalku = new Kalkulator();
        kalku.setEnergi(energi);
        kalku.setSem(sem);
        double energiBuSui = 0;
        if (kalku.getSem().equals("s1")) {
            energiBuSui = kalku.getEnergi() + 500;
            kalku.setEnergi(energiBuSui);
        } else if (kalku.getSem().equals("s2")) {
            energiBuSui = kalku.getEnergi() + 550;
            kalku.setEnergi(energiBuSui);
        }
        return kalku.getEnergi();
    }

    public double methodBulat(double nilai) {
        double hasil = nilai;
        int decimalPlace = 4;
        BigDecimal bd = new BigDecimal(hasil);
        bd = bd.setScale(decimalPlace, BigDecimal.ROUND_UP);
        hasil = bd.doubleValue();
        return hasil;
    }
}